package com.ita.jmsmain;

import java.util.ArrayList;
import java.util.List;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;

import com.ita.dom.dao.IWeiBoDao;
import com.ita.dom.dao.impl.WeiBoDaoImpl;
import com.ita.dom.entity.WeiBo;
import com.ita.dom.handler.WeiBoReadeJsonHandler;

public class PubSubConsumer {

	public static void main(String[] args) throws JMSException {
		// TODO Auto-generated method stub

		ConnectionFactory cf=new ActiveMQConnectionFactory(
				"failover:tcp://localhost:61616");
		System.out.println(cf);
		Connection conn=cf.createConnection();
		Session session=conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
		Destination queue=new ActiveMQQueue("0719");
		MessageConsumer consumer=session.createConsumer(queue);
		
		conn.start();
		List<WeiBo> list = new ArrayList<WeiBo>();
		System.out.println("hello");
		TextMessage tm = (TextMessage) consumer.receive();
		System.out.println(tm.getText());
		WeiBoReadeJsonHandler readJson=new WeiBoReadeJsonHandler();
		list=readJson.readJson(tm.getText());
		System.out.println(list.size());
		IWeiBoDao dao=new WeiBoDaoImpl();
		dao.insert(list);
		
		System.out.println("msg....");
		
		consumer.close();
		session.close();
		conn.close();
	}

}
